package com.wangtao.firstapp.activity.sife_secondactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.ThreeSecondContentAdapter;
import com.wangtao.firstapp.custom.CollectDialog;
import com.wangtao.firstapp.custom.FullyLinearLayoutManager;
import com.wangtao.firstapp.modles.ThreeSifeSecond;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 二级页面-->小唐君无图页面
 * Created by lanouhn on 16/8/30.
 */
public class ThreeSifeSecondActivity extends Activity implements View.OnClickListener {

    private ImageButton back, like, ivshare,like_yellow;

    //文章，图，卖东西,评论
    private ThreeSifeSecond threeSifeSecond;
    private ThreeSecondContentAdapter threeSecondContentAdapter;
    private RecyclerView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_sife_second_three);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        back = (ImageButton) findViewById(R.id.ibback_act_sife_second_three);
        like = (ImageButton) findViewById(R.id.like_act_sife_second_three);
        ivshare = (ImageButton) findViewById(R.id.ivshare_act_sife_second_three);
        like_yellow= (ImageButton) findViewById(R.id.like_title_bar_like_yellow);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview_act_sife_second_essay_three);
    }

    private void initData() {
        Intent intent = getIntent();

        String item_ID=intent.getStringExtra("item_ID");
        //文章，图，卖东西,评论
        String urlSecond = "http://open4.bantangapp.com/topic/newInfo";
        String paramsSecond = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + item_ID + "&trace_id=&relate_pid=&statistics_uv=1&is_night=0";

        try {
            HttpUtils.doPostAsyn(urlSecond, paramsSecond, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    threeSifeSecond = gson.fromJson(result, ThreeSifeSecond.class);

                    if (threeSifeSecond.getData().getPost_list().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }


                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    threeSecondContentAdapter = new ThreeSecondContentAdapter(ThreeSifeSecondActivity.this, threeSifeSecond);
                    recyclerView.setAdapter(threeSecondContentAdapter);
                    recyclerView.setLayoutManager(new FullyLinearLayoutManager(ThreeSifeSecondActivity.this));
                    break;
            }
        }
    };

    private void initEvent() {
        back.setOnClickListener(this);
        like.setOnClickListener(this);
        ivshare.setOnClickListener(this);
        like_yellow.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回上一个页面
            case R.id.ibback_act_sife_second_three:
                finish();
                break;
            //第一次点击，收藏成功
            case R.id.like_act_sife_second_three:
                like.setVisibility(View.INVISIBLE);
                like_yellow.setVisibility(View.VISIBLE);
                CollectDialog collectDialogSusseed=new CollectDialog(ThreeSifeSecondActivity.this)
                        .showDialog(R.layout.dialog_collect_succeed,0,0);
                break;
//            //分享
//            case R.id.ivshare_act_sife_second_three:
//                Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
//                break;
            //第二次点击，取消收藏
            case R.id.like_title_bar_like_yellow:
                like.setVisibility(View.VISIBLE);
                like_yellow.setVisibility(View.INVISIBLE);
                CollectDialog collectDialogCalloff=new CollectDialog(ThreeSifeSecondActivity.this)
                        .showDialog(R.layout.dialog_callof_collect,0,0);
                break;

        }
    }
}
